//
//  TQYSocketManager.m
//  TQY
//
//  Created by 甘世清 on 2024/10/26.
//

#import "TQYSocketManager.h"
#import "TQYWebSocket.h"
#import "IMMessageManager.h"

@implementation TQYSocketManager

+(void)SQ_SocketManager{
    [IMSendMessageManager emjonDict];
    NSString *url = [NSString stringWithFormat:@"wss://taoqingyan.qingtao.fun/taoqingyanws/im"];
    [[TQYWebSocket shareManager] SQ_open:url connect:^{
        DTLog(@"链接成功---%@",[NSString SQ_currentTime]);
    } receive:^(id  _Nonnull message, SQSocketReceiveType type) {
        [TQYSocketManager SQ_message:message];
    } failure:^(NSError * _Nonnull error) {
        DTLog(@"---失败了---%@",error);
    }];
}
+(void)SQ_message:(NSString*)message{
    NSString * string = [NSString stringWithFormat:@"%@",message];
    NSDictionary * dic = [[NSDictionary alloc]initWithDictionary:[string mj_JSONObject]];
    DTLog(@"接收的消息 ---%@",dic);
    NSString * cmd = [NSString stringWithFormat:@"%@",[dic objectForKey:@"cmd"]];
    if (![cmd isNotEmpty]) {
        return;
    }
    if ([cmd isEqualToString:@"0"]) {//登录
        
    } else   if ([cmd isEqualToString:@"1"]) {//心跳
        
    } else   if ([cmd isEqualToString:@"2"]) {//强制下线
        
    } else   if ([cmd isEqualToString:@"3"]) {//私聊消息
       
        NSMutableDictionary * data = [[NSMutableDictionary alloc]initWithDictionary:[dic objectForKey:@"data"]]; //发来的数据
        [self SQ_messageSetUP:data];
       
    }else   if ([cmd isEqualToString:@"4"]) {//群聊消息
        
    }else   if ([cmd isEqualToString:@"5"]) {//系统消息
        
    }
}
+(void)SQ_messageSetUP:(NSMutableDictionary*)data{
    NSString  * sendId = [NSString stringWithFormat:@"%@",[data objectForKey:@"sendId"]]; //发送者id
    if (![sendId isNotEmpty]) {
        return;
    }
    NSString * sendTime = [NSString stringWithFormat:@"%@",[data objectForKey:@"sendTime"]];
    if (sendTime.length > 11) {
        [data setObject:[sendTime substringToIndex:10] forKey:@"sendTime"];
    }
    NSString  * recvId = [NSString stringWithFormat:@"%@",[data objectForKey:@"recvId"]]; //接收者id
   
    NSString * extend = [NSString stringWithFormat:@"%@",[data objectForKey:@"extend"]];//扩展
    NSString * content = [NSString stringWithFormat:@"%@",[data objectForKey:@"content"]];
    content = [RSA decryptString:content publicKey:PUBLICKey];
    DTLog(@"-------解密数据----%@",content);
    [data setObject:content forKey:@"content"];
    //消息模型
    IMMessageModel * model = [IMMessageModel mj_objectWithKeyValues:data];
    [Manager SQ_SetObject:model.messageId FormKey:[TQYFMDBManager SQ_getminIDName]];
        model.toUser =  sendId;
    if ([sendId isEqualToString:[UserModel share].userid]) {
        model.toUser =  recvId;
 
    }
    NSString  * roomID = [NSString stringWithFormat:@"%@_%@",[UserModel share].userid,model.toUser];//房间id
    
    BOOL isAddMessage = [TQYFMDBManager SQ_messageModel:model.messageId roomId:roomID];
    if ([extend isNotEmpty]) {
        NSDictionary * extendDic = [extend mj_JSONObject];
        if ([model.type isEqualToString:@"1"]) {//如果是图片
            NSString * imageWidth = [NSString stringWithFormat:@"%@",[extendDic objectForKey:@"imageWidth"]];
            NSString * imageHeight = [NSString stringWithFormat:@"%@",[extendDic objectForKey:@"imageHeight"]];
            model.imageWidth = [imageWidth floatValue];
            model.imageHeight = [imageHeight floatValue];
        }else  if ([model.type isEqualToString:@"5"]) {//如果是定位
            NSString * lat = [NSString stringWithFormat:@"%@",[extendDic objectForKey:@"lat"]];
            NSString * lng = [NSString stringWithFormat:@"%@",[extendDic objectForKey:@"lng"]];
            model.lat = [lat floatValue];
            model.lng = [lng floatValue];
            model.address = [NSString stringWithFormat:@"%@",[extendDic objectForKey:@"address"]];
        }
    }

    IMListModel * listModel = [TQYFMDBManager SQ_roomIDGetIMListModel:roomID];
    listModel.roomId = roomID;//房间id
    listModel.type = model.type;//消息类型
    listModel.content = model.content;//消息
    listModel.sendTime  = model.sendTime;//时间
    listModel.toUser = model.toUser;//对方id
    if ([listModel.toUserPortrait isNotEmpty]) {
        if (!isAddMessage) { //如果不存在,就+1
            listModel.messageCount += 1;//未读数
        }
        model.toUserPortrait = listModel.toUserPortrait;
        model.toUserName = listModel.toUserName;
       //有头像 就更新消息时间,最后内容就可以了
        [TQYFMDBManager SQ_addListModel:listModel];//存储会话
        [TQYFMDBManager SQ_addListIMMessage:model];//存储消息
    } else {
       //没有头像,就请求用户信息
        [self SQ_getPeopleInfo:listModel message:model];
    }
}
+(void)SQ_getPeopleInfo:(IMListModel*)model message:(IMMessageModel*)messageList{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:model.toUser forKey:@"uid"];
    [HttpTool post:@"user/userInfo" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        PeopleModel * peopleModel = [PeopleModel mj_objectWithKeyValues:httpModel.data];
        model.toUserName = peopleModel.name;
        model.toUserPortrait = peopleModel.portrait;
        model.messageCount = 1;
        messageList.toUserPortrait = model.toUserPortrait;
        messageList.toUserName = model.toUserName;
        [TQYFMDBManager SQ_addListModel:model];//存储会话
        [TQYFMDBManager SQ_addListIMMessage:messageList];//存储消息
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}

+(void)SQ_getpullOfflineMessage{
    //告知后台拉起离线消息
    NSString * minID = [Manager SQ_stringForKey:[TQYFMDBManager SQ_getminIDName]];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    if ([minID isNotEmpty]) {
        [dic setObject:minID forKey:@"minId"];
    }else{
        return;
    }
    [HttpTool post:@"im/message/private/pullOfflineMessage" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}

@end
